package android.Spider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class PollReceive extends Activity {

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		setContentView(R.layout.questionreceive);

        m_connection.Initialize(this);

        m_question = Questions.Instance(this).GetUnanswered();
		FillView(m_question);

		Button btn;
		for (int i = 0; i < 6; ++i)
		{
			btn = (Button)findViewById(BtnIDs[i]);
			btn.setOnClickListener(m_lsnOption);
		}
	}

	@Override
	protected void onDestroy() {
		m_connection.Unregister();
		super.onDestroy();
	}

	private void FillView(Questions.Question question)
	{
		if (question != null)
		{
			TextView txtFrom = (TextView)findViewById(R.id.EtQueryPollFrom);
			String strFrom = new String(question.m_lstFrom.get(0));
			for (int i = 1; i < question.m_lstFrom.size(); ++i)
				strFrom += " -> " + question.m_lstFrom.get(i);
			
			txtFrom.setText(strFrom);
			
			EditText edtMsg = (EditText)findViewById(R.id.EtQueryPollMessage);
			edtMsg.setText(question.m_strMsg);
			
			int nVisibleIndex = 0;
			m_nBtnCount = question.m_lstOptions.size();
			boolean visible[] = Visibility[m_nBtnCount - 2];
			for (int nButton = 0; nButton < 6; ++nButton)
			{
				Button btn = (Button)findViewById(BtnIDs[nButton]);
				if (visible[nButton])
				{
					btn.setText("" + (nVisibleIndex + 1) + ". " + question.m_lstOptions.get(nVisibleIndex));
					btn.setVisibility(View.VISIBLE);
					++nVisibleIndex;
				}
				else
				{
					btn.setVisibility(View.INVISIBLE);
				}
			}
		}
		else
		{
			// Disable and hide all controls
			m_nBtnCount = 0;
			for (int nButton = 0; nButton < 6; ++nButton)
			{
				Button btn = (Button)findViewById(BtnIDs[nButton]);
				btn.setVisibility(View.INVISIBLE);
			}
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Forward", R.drawable.forward);
	    menu.add(0, 1, "Later", R.drawable.later);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
	    switch (item.getId())
	    {
	    case 0:
	    	Intent intent = new Intent(this, PollSend.class);
	    	Bundle bundle = new Bundle();
	    	bundle.putLong(PollSend.s_strQuestionID, m_question.m_idQuestion);
	    	bundle.putString(PollSend.s_strMsg, m_question.m_strMsg);
	    	int nCount = m_question.m_lstOptions.size();
	    	bundle.putInt(PollSend.s_strOptionsCount, nCount);
	    	for (int i = 0; i < nCount; ++i)
	    		bundle.putString(PollSend.s_strOption + i, m_question.m_lstOptions.get(i));
	    	
	    	intent.putExtras(bundle);
	    	startSubActivity(intent, 0);
	    	break;
	    case 1:
	    	finish();
	    	break;
	    default:
	    	return false;
	    }
	    
	    return true;
	}

    private OnClickListener m_lsnOption = new OnClickListener()
    {
    	public void onClick(View v)
    	{
    		if (m_question != null)
    		{
        		int id = v.getId();
        		int nIndex = 0;
        		while (id != BtnIDs[nIndex] && nIndex < 6)
        			++nIndex;

        		if (nIndex < 6)			// This has to be true.
        		{
        			int nOption = OptionIndex[m_nBtnCount - 2][nIndex];
        			
        			PollReceive.this.m_connection.SendAnswer(m_question.m_lstFrom.get(0), m_question.m_idQuestion, nOption);

            		PollReceive.this.m_connection.Unregister();
            		PollReceive.this.finish();
            		
            		if (m_brMother != null)
            			m_brMother.UpdateReceivedInquires();
        		}
    		}
    	}
    };

    private int 				m_nBtnCount = 0;
    private Questions.Question 	m_question = null;
    
    private final static boolean Visibility[][] = new boolean[][] {
    	new boolean[] { false, true, false, false, true, false },
    	new boolean[] { true, true, true, false, false, false },
    	new boolean[] { true, true, false, true, true, false },
    	new boolean[] { true, true, true, true, true, false },
    	new boolean[] { true, true, true, true, true, true }
    };

    private final static int OptionIndex[][] = new int[][] {
    	new int[] { -1, 0, -1, -1, 1, -1 },
    	new int[] { 0, 1, 2, -1, -1, -1 },
    	new int[] { 0, 1, -1, 2, 3, -1 },
    	new int[] { 0, 1, 2, 3, 4, -1 },
    	new int[] { 0, 1, 2, 3, 4, 5}
    };

	private final static int BtnIDs[] = new int[] { R.id.AnswerButton1, R.id.AnswerButton2, R.id.AnswerButton3, 
			R.id.AnswerButton4, R.id.AnswerButton5, R.id.AnswerButton6};

    private XmppConnection m_connection = new XmppConnection()
    {
		@Override
		public void OnConnected()
		{
			for (int i = 0; i < 6; ++i)
			{
				View view = PollReceive.this.findViewById(BtnIDs[i]);
				view.setEnabled(true);
			}
		}
		@Override
		public void OnDisconnected()
		{
			for (int i = 0; i < 6; ++i)
			{
				View view = PollReceive.this.findViewById(BtnIDs[i]);
				view.setEnabled(false);
			}
		}
    };
    
    public static Broodmother m_brMother = null;
}
